/**
 * Copyright 2020 Alibaba Group Holding Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.maxgraph.common.util;

import java.security.SecureRandom;
import java.util.Base64;

public class UuidUtils {

    private static class SecureRandomHolder {
        static final SecureRandom INSTANCE = new SecureRandom();
    }

    public static String getBase64UUIDString() {
        byte[] bytes = new byte[16];
        SecureRandomHolder.INSTANCE.nextBytes(bytes);
        // UUID v4. (http://www.ietf.org/rfc/rfc4122.txt)
        // clear version of 4 msb
        bytes[6] &= 0x0f;
        // set version to 4
        bytes[6] |= 0x40;
        // clear variant of 2 msb
        bytes[8] &= 0x3f;
        // set variant
        bytes[8] |= 0x80;
        // return base64 encoding
        return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
    }
}
